﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using TangoGames;
using TangoGames.Base;
using TangoGames.Scene;
using incubation_xna3d.HUD;
using incubation_xna3d.Config;


namespace incubation_xna3d.Scene
{
    class CameraControl:GameComponent,ICameraControl
    {

        #region Fields
        
        private static float ANGLE_FACTOR = 1.0f;

        private ICamera camera;

        private Hud hud;

        IInputService input;
        IDefaults defaults;
        
        #endregion

        #region Properties

        public ICamera Camera
        {
            get { return camera; }
            set { camera = value; }
        }

        #endregion


        public CameraControl(Game game, ICamera camera,Hud hud)
            : base(game)
        {
            this.camera = camera;
            this.hud = hud;

            //game.Components.Add(this);

            //this.UpdateOrder = 10000;

            input = (IInputService)Game.Services.GetService(typeof(IInputService));
            defaults = (IDefaults)Game.Services.GetService(typeof(IDefaults));

            //inicializa camera
            InitCamera();

        }


        private void InitCamera() 
        { 
            var cameraInfo = defaults.Camera;

            this.camera.Position = cameraInfo.Position;
            this.camera.LookAt(cameraInfo.Target);
            this.camera.NearPlane = cameraInfo.NearPlane;
            this.camera.FarPlane = cameraInfo.FarPlane;
            this.camera.Angle = cameraInfo.Angle;
        }

        public override void Update(GameTime gameTime)
        {

            UpdateInput();
            UpdateInputHud();

            base.Update(gameTime);

        }

        private void UpdateInputHud()
        {
            //botao de vira camera p esquerda
            if (input.MouseClick(hud.Localturnleft()))
            {
                Console.WriteLine("clicou vira esquerda");
                Camera.Angle -= MathHelper.ToRadians(45);
            }
            //botao de vira camera p direita
            if (input.MouseClick(hud.Localturnright()))
            {
                Console.WriteLine("clicou vira direita");
                Camera.Angle += MathHelper.ToRadians(45);
            }

        }

        private void UpdateInput()
        {
            if (input.KeyPressOnce(Keys.LeftAlt ) || input.KeyPressOnce(Keys.LeftAlt ))
            {
                input.CenterMouse = true;

            }

            if (input.teclado.IsKeyDown(Keys.LeftAlt) || input.teclado.IsKeyDown(Keys.LeftAlt))
            {

                Point mousevar = input.MouseCenterDiff();
                float deltaX = (mousevar.X * ANGLE_FACTOR);
                float deltaY = (mousevar.Y * ANGLE_FACTOR);
                this.camera.LookMove( deltaX, deltaY);
            }
            else
            {
                if (input.CenterMouse) input.CenterMouse = false;
            }
         


            // deslocamento da camera esta uma merda
            // agora não esta mais


            if (input.mouse.ScrollWheelValue > input.mouse_anterior.ScrollWheelValue)
            {

                this.camera.Position -= new Vector3(0, 2.5f, 0);
                this.Camera.Target -= new Vector3(0, 2.5f, 0);
               
            }
            if (input.mouse.ScrollWheelValue < input.mouse_anterior.ScrollWheelValue)
            {

                this.camera.Position += new Vector3(0, 2.5f, 0);
                this.Camera.Target += new Vector3(0, 2.5f, 0);
                
            }

            if(input.teclado.IsKeyDown(Keys.C))
            {
                InitCamera();
            }

            if (input.teclado.IsKeyDown(Keys.D)|| input.mouse.X >= Game.Window.ClientBounds.Right - 5 )
            {
                Vector3 deslocamento;

                deslocamento = camera.XAxis;

                deslocamento.Normalize();

               
                deslocamento *= new Vector3(2.0f, 0, 2.0f);
                camera.Position += deslocamento;
                this.Camera.Target += deslocamento;
   

            }


            if (input.teclado.IsKeyDown(Keys.A) || input.mouse.X <= Game.Window.ClientBounds.Left + 5)
            {
                Vector3 deslocamento;

                deslocamento = camera.XAxis;

                deslocamento.Normalize();
               
                deslocamento *= new Vector3(2.0f, 0, 2.0f);
                camera.Position -= deslocamento;
                this.Camera.Target -= deslocamento;
    
            }
            if (input.teclado.IsKeyDown(Keys.S) || input.mouse.Y >= Game.Window.ClientBounds.Bottom - 5)
            {
                Vector3 deslocamento;

                deslocamento = camera.ZAxis;

                deslocamento.Normalize();

                deslocamento *= new Vector3(2.0f, 0, 2.0f);

                camera.Position += deslocamento;

            }

            if (input.teclado.IsKeyDown(Keys.W) || input.mouse.Y <= Game.Window.ClientBounds.Top + 5)
            {
                Vector3 deslocamento;

                deslocamento = camera.ZAxis;

                deslocamento.Normalize();

                deslocamento *= new Vector3(2.0f, 0, 2.0f);

                camera.Position -= deslocamento;

            }


        }

    }
}
